﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using Opo.Net.Mail;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Manages the validation of users on a pop3 server
    /// </summary>
    public class Pop3MembershipProvider : MembershipProvider
    {
        IPop3Client _pop3Client;

        /// <summary>
        /// Initializes a new instance of the <see cref="Pop3MembershipProvider"/> class.
        /// </summary>
        /// <param name="pop3Client">The POP3 client.</param>
        public Pop3MembershipProvider(IPop3Client pop3Client)
        {
            if (pop3Client == null)
            {
                throw new ArgumentNullException("pop3Client");
            }
            _pop3Client = pop3Client;
        }

        /// <summary>
        /// The name of the application using the custom membership provider.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The name of the application using the custom membership provider.
        /// </returns>
        public override string ApplicationName { get; set; }

        /// <summary>
        /// Verifies that the specified user name and password exist in the data source.
        /// </summary>
        /// <param name="username">The name of the user to validate.</param>
        /// <param name="password">The password for the specified user.</param>
        /// <returns>
        /// True if the specified username and password are valid; otherwise, false.
        /// </returns>
        public override bool ValidateUser(string username, string password)
        {
            _pop3Client.Username = username;
            _pop3Client.Password = password;

            bool isValidated = false;

            try
            {
                _pop3Client.Connect();
                isValidated = _pop3Client.Login();
                _pop3Client.Disconnect();
            }
            catch (Exception) { }

            return isValidated;
        }

        // not supported methods
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override bool EnablePasswordReset
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override bool EnablePasswordRetrieval
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override int GetNumberOfUsersOnline()
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override string GetPassword(string username, string answer)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override string GetUserNameByEmail(string email)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override int MinRequiredPasswordLength
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override int PasswordAttemptWindow
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override bool RequiresUniqueEmail
        {
            get { throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported."); }
        }
        public override string ResetPassword(string username, string answer)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override bool UnlockUser(string userName)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
        public override void UpdateUser(MembershipUser user)
        {
            throw new NotSupportedException("This method is not supported by the Pop3MembershipProvider. Only ValidateUser(username, password) is supported.");
        }
    }
}
